<?php

namespace Home\Controller;

use Think\Controller;

class SharingController extends BaseController
{

    /*
     * upFile页面接口
     *
     * */
    //文件上传
    public function up_file(){
        if($_POST['user_id']=='[object Undefined]'){
            $return_data=array();
            $return_data['error_code']=1;
            $return_data['msg']='参数不足: 未获取到用户的id';
            $this->ajaxReturn($return_data);
        }
        if($_POST['file_url']==""){
            $return_data=array();
            $return_data['error_code']=2;
            $return_data['msg']='参数不足: 未获取到临时文件地址';
            $return_data['debug']=$_FILES;
            $this->ajaxReturn($return_data);
        }

        //在C:\wamp64\bin\apache\apache2.4.41\bin\php.ini 839行 文件修改upload_max_filesize字段来设置上传文件上限
        //$this->ajaxReturn($_FILES['file']['tmp_name']);//小程序传来的文件在系统中的临时文件路径
        if(is_uploaded_file($_FILES['file']['tmp_name'])) {//该临时文件是可以上传的文件
            $file_name=$_POST['file_time'].$_POST['file_name'];//存储文件名为:时间戳+文件名

            //存储地址设置成每个用户有一个文件夹
            $file_path="C:/SharingFiles/".$_POST['user_id'];
            if(!is_dir($file_path))mkdir($file_path);//文件夹是否存在
            $save_path=$file_path."/".$file_name;//存储地址+文件名

//            $this->ajaxReturn($save_path);
            //移动临时文件到指定文件夹
            if(move_uploaded_file($_FILES['file']['tmp_name'],$save_path)){// 上传成功
                //将文件数据插入表file_info
                $file_info=M('file_info');
                $data=array();
                $data['user_id']=$_POST['user_id'];
                $data['user_school']=$_POST['user_school'];
                $data['user_faculty']=$_POST['user_faculty'];
                $data['user_name']=$_POST['user_name'];
                $data['user_picurl']=$_POST['user_picurl'];
                $data['type']=$_POST['type'];
                $data['author']=$_POST['author'];
                $data['remark']=$_POST['remark'];
                $data['gold_num']=$_POST['gold_num'];
                $data['file_url']=$save_path;//文件下载地址
                $data['file_name']=$_POST['file_name'];
                $data['file_time']=$_POST['file_time'];
                $data['file_size']=$_POST['file_size'];
                $data['file_suffix']=$_POST['file_suffix'];
                $res=$file_info->add($data);//插入数据
                if($res){
                    $return_data=array();
                    $return_data['error_code']=0;
                    $return_data['msg']='文件上传成功';
                    $return_data['debug']=$_FILES;
                    $this->ajaxReturn($return_data);
                }else{
                    $return_data=array();
                    $return_data['error_code']=3;
                    $return_data['msg']='数据添加失败';
                    $this->ajaxReturn($return_data);
                }
            }
            $return_data=array();
            $return_data['error_code']=3;
            $return_data['msg']='文件上传失败';
            $this->ajaxReturn($return_data);
        }
        $return_data=array();
        $return_data['error_code']=3;
        $return_data['msg']='无法上传该文件';
        $this->ajaxReturn($return_data);
    }



    /*
     * searchFile页面接口
     *
     * */
    //根据搜索内容获取结果【未完成】
    public function search(){
        $file_info=M('file_info');

        //设置查询条件
        $keyword=$_POST['search_detail'];
//        $condition=array();
        $condition['file_name'] = array('like','%'.$keyword.'%');


        //按照时间倒序获取搜索文件信息
        $all_files=$file_info->where($condition)->order('file_id desc')->select();

        //将所有的时间戳转换成2011-12-12 02:20:00格式
        foreach($all_files as $key=>$obj){
            $all_files[$key]['file_time']=date('Y-m-d',$obj['file_time']);
        }
        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="数据获取成功";
        $return_data['count']=count($all_files);
        $return_data['data']=$all_files;
        $this->ajaxReturn($return_data);
    }

    //获取所有文件信息
    public function get_all_files(){
        $file_info=M('file_info');

        //设置查询条件

        //按照时间倒序获取所有文件信息
        $all_files=$file_info->order('file_id desc')->select();

        //将所有的时间戳转换成2011-12-12 02:20:00格式
        foreach($all_files as $key=>$obj){
            $all_files[$key]['file_time']=date('Y-m-d',$obj['file_time']);
        }
        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="数据获取成功";
        $return_data['count']=count($all_files);
        $return_data['data']=$all_files;
        $this->ajaxReturn($return_data);

    }

    //获取用户同学校的文件信息
    public function get_mySchool_files(){
        $file_info=M('file_info');

        //设置查询条件
        $condition=array();
        $condition['user_school']=$_POST['user_school'];
        //按照时间倒序获取所有文件信息
        $all_files=$file_info->where($condition)->order('file_id desc')->select();

        //将所有的时间戳转换成2011-12-12 02:20:00格式
        foreach($all_files as $key=>$obj){
            $all_files[$key]['file_time']=date('Y-m-d',$obj['file_time']);
        }
        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="数据获取成功";
        $return_data['count']=count($all_files);
        $return_data['data']=$all_files;
        $this->ajaxReturn($return_data);
    }

    //获取用户同学院的文件信息
    public function get_myFaculty_files(){
        $file_info=M('file_info');

        //设置查询条件
        $condition=array();
        $condition['user_school']=$_POST['user_school'];
        $condition['user_faculty']=$_POST['user_faculty'];
        //按照时间倒序获取所有文件信息
        $all_files=$file_info->where($condition)->order('file_id desc')->select();

        //将所有的时间戳转换成2011-12-12 02:20:00格式
        foreach($all_files as $key=>$obj){
            $all_files[$key]['file_time']=date('Y-m-d',$obj['file_time']);
        }
        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="数据获取成功";
        $return_data['count']=count($all_files);
        $return_data['data']=$all_files;
        $this->ajaxReturn($return_data);
    }

    //获取以金币排序的文件信息
    public function get_gold_files(){
        $file_info=M('file_info');

        //设置查询条件
        $condition=array();
        if($_POST['mySchool']==1){
            $condition['user_school']=$_POST['user_school'];
        }
        if($_POST['myFaculty']==1){
            $condition['user_school']=$_POST['user_school'];
            $condition['user_faculty']=$_POST['user_faculty'];
        }
        if($_POST['gold']==2){
            //按照金币数量从大到小获取所有文件信息
            $all_files=$file_info->where($condition)->order('gold_num desc')->select();
        }
        else if($_POST['gold']==1){
            //按照金币数量从小到大获取所有文件信息
            $all_files=$file_info->where($condition)->order('gold_num')->select();
        }
        else{
            //按照时间倒序获取所有文件信息
            $all_files=$file_info->where($condition)->order('file_id desc')->select();
        }
        //将所有的时间戳转换成2011-12-12 02:20:00格式
        foreach($all_files as $key=>$obj){
            $all_files[$key]['file_time']=date('Y-m-d',$obj['file_time']);
        }
        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="数据获取成功";
        $return_data['count']=count($all_files);
        $return_data['data']=$all_files;
        $this->ajaxReturn($return_data);
    }

    //获取以下载量排序的文件信息
    public function get_down_files(){
        $file_info=M('file_info');

        //设置查询条件
        $condition=array();
        if($_POST['mySchool']==1){
            $condition['user_school']=$_POST['user_school'];
        }
        if($_POST['myFaculty']==1){
            $condition['user_school']=$_POST['user_school'];
            $condition['user_faculty']=$_POST['user_faculty'];
        }
        if($_POST['down']==2){
            //按照金币数量从大到小获取所有文件信息
            $all_files=$file_info->where($condition)->order('down_num desc')->select();
        }
        else if($_POST['down']==1){
            //按照金币数量从小到大获取所有文件信息
            $all_files=$file_info->where($condition)->order('down_num')->select();
        }
        else{
            //按照时间倒序获取所有文件信息
            $all_files=$file_info->where($condition)->order('file_id desc')->select();
        }
        //将所有的时间戳转换成2011-12-10格式
        foreach($all_files as $key=>$obj){
            $all_files[$key]['file_time']=date('Y-m-d',$obj['file_time']);
        }
        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="数据获取成功";
        $return_data['count']=count($all_files);
        $return_data['data']=$all_files;
        $this->ajaxReturn($return_data);
    }



    /*
     * fileDetail页面接口
     *
     * */
    //获取一个文件信息
    public function  get_one_file(){

        $file_info=M('file_info');
        $condition=array();
        $condition['file_id']=$_POST['file_id'];
        $file=$file_info->where($condition)->find();//查询一条数据

        //显示已读
        if($_POST['have_read']=="0"){
            $gold_moving=M('gold_moving');
            $where=array();
            $where['gold_id']=(int)$_POST['gold_id'];

            $data=array();
            $data['have_read']=1;
            $res=$gold_moving->where($where)->save($data);
        }

        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="数据获取成功";
        $return_data['data']=$file;
        $this->ajaxReturn($return_data);
    }

    //点赞
    public function like(){
        $file_info=M('file_info');
        //查询条件
        $condition=array();
        $condition['file_id']=$_POST['file_id'];
        $result=$file_info->where($condition)->find();

        //修改数据
        $data=array();
        $data['like_num']=$result['like_num']+1;
        $file_info->where($condition)->save($data);

        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="点赞成功";
        $this->ajaxReturn($return_data);
    }

    //取消赞
    public function cancel_like(){
        $file_info=M('file_info');
        //查询条件
        $condition=array();
        $condition['file_id']=$_POST['file_id'];
        $result=$file_info->where($condition)->find();

        //修改数据
        $data=array();
        $data['like_num']=$result['like_num']-1;
        $file_info->where($condition)->save($data);

        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="点赞成功";
        $this->ajaxReturn($return_data);
    }

    //文件下载
    public function down_file()
    {
        $file_info=M('file_info');
        //查询条件
        $condition=array();
        $condition['file_id']=$_GET['file_id'];
        $result=$file_info->where($condition)->find();
//        dump($result);
        $file_name = $result['file_name'];//下载文件名
//        dump($file_name);
        $file_dir = $result['file_url'];//下载文件存放目录
//        dump($file_dir);
        $file_type=$result['file_suffix'];//文件类型
        //检查文件是否存在
        if (!file_exists($file_dir)) {
            $return_data=array();
            $return_data['error_code']=1;
            $return_data['msg']="文件不存在";
            $this->ajaxReturn($return_data);
        } else {
            //以只读和二进制模式打开文件
            $file = fopen($file_dir, "rb");

            if($file_type==".doc"||$file_type==".docx"){
                $type="msword";
            }
            else if($file_type==".xls"){
                $type="vnd.ms-excel";
            }
            else if($file_type==".xlsx"){
                $type="vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            }
            else if($file_type==".ppt"){
                $type="vnd.ms-powerpoint";
            }
            else if($file_type==".pptx"){
                $type="vnd.openxmlformats-officedocument.presentationml.presentation";
            }
            else if($file_type==".pdf"){
                $type="pdf";
            }
            else{
                $type="octet-stream";//文件流格式
            }
//            dump($type);
            //告诉浏览器文件的文件格式
            Header("Content-type: application/$type");

            //请求范围的度量单位
            Header("Accept-Ranges: bytes");

            //Content-Length是指定包含于请求或响应中数据的字节长度
            Header("Accept-Length: " . filesize($file_dir));
            //用来告诉浏览器，文件是可以当做附件被下载，下载后的文件名称为$file_name该变量的值。
            Header("Content-Disposition: attachment; filename=" . $file_name);

            //读取文件内容并直接输出到浏览器
            echo fread($file, filesize($file_dir));
            fclose($file);
            exit ();
        }
    }

    //文件下载量+1
    public function add_down_num(){

        //文件下载量+1
        $file_info=M('file_info');
        //查询条件
        $condition=array();
        $condition['file_id']=$_POST['file_id'];
        $result=$file_info->where($condition)->find();

        if($result['user_id']!=$_POST['my_id']){
            //修改数据
            $data=array();
            $data['down_num']=$result['down_num']+1;
            $file_info->where($condition)->save($data);

            //提醒被下载用户和下载用户的金币动态
            if($_POST['gold_num']!=0){//双方金币有变化且不是下载自己上传的文件
                $gold_moving=M('gold_moving');

                //两用户的金币量加减
                //查询条件
                $user_list=M('user_list');
                $condition1=array();
                $condition1['user_id']=$_POST['user_id'];
                $result=$user_list->where($condition1)->find();

                //修改数据
                $data1=array();
                $data1['gold_all_num']=$result['gold_all_num']+$_POST['gold_num'];//被下载的用户金币+
                $user_list->where($condition1)->save($data1);

                //查询条件
                $condition2=array();
                $condition2['user_id']=$_POST['my_id'];
                $result=$user_list->where($condition2)->find();

                //修改数据
                $data2=array();
                $data2['gold_all_num']=$result['gold_all_num']-$_POST['gold_num'];//下载的用户金币-
                $user_list->where($condition2)->save($data2);

                //被下载用户
                $condition3=array();
                $condition3['my_id']=$_POST['user_id'];
                $condition3['other_id']=$_POST['my_id'];
                $r1=$gold_moving->where($condition3)->select();
                $num1=count($r1);$num1++;
                if($num1>10){//消息满十，需删除最早的消息
                    $gold=array();
                    $gold['gold_id']=$r1[0]['gold_id'];
                    $t=$gold_moving->where($gold)->delete();
                }
                $data3=array();
                $data3['my_id']=$_POST['user_id'];
                $data3['other_id']=$_POST['my_id'];
                $data3['file_id']=$_POST['file_id'];
                $data3['move_time']=$_POST['move_time'];
                $data3['gold_move_num']=$_POST['gold_num'];
                $data3['other_name']=$_POST['name2'];
                $t1=$gold_moving->add($data3);

                //下载用户
                $condition4=array();
                $condition4['my_id']=$_POST['my_id'];
                $condition4['other_id']=$_POST['user_id'];
                $r2=$gold_moving->where($condition4)->select();
                $num2=count($r2);$num2++;
                if($num2>10){//消息满十，需删除最早的消息
                    $gold=array();
                    $gold['gold_id']=$r2[0]['gold_id'];
                    $t=$gold_moving->where($gold)->delete();
                }
                $data4=array();
                $data4['my_id']=$_POST['my_id'];
                $data4['other_id']=$_POST['user_id'];
                $data4['file_id']=$_POST['file_id'];
                $data4['move_time']=$_POST['move_time'];
                $data4['gold_move_num']=-$_POST['gold_num'];
                $data4['other_name']=$_POST['name1'];
                $t2=$gold_moving->add($data4);
            }
        }

        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="文件下载量+1，金币修改成功,金币动态更新";
        $this->ajaxReturn($return_data);
    }



    /*
     * goldNews页面接口
     *
     * */
    //获取所有金币动态
    public function get_gold_moving(){

        $gold_moving=M('gold_moving');
        $condition=array();
        $condition['my_id']=$_POST['my_id'];
        $result=$gold_moving->where($condition)->order('move_time desc')->select();

        foreach ($result as $key =>$i){
            //转化时间戳
            $result[$key]['move_time'] = date('Y-m-d H:i',$i['move_time']);

            $result[$key]['gold_move_num']=(int)$i['gold_move_num'];//类型转换

        }

        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="数据获取成功";
        $return_data['data']=$result;
        $this->ajaxReturn($return_data);
    }


    /*
     * mine页面接口
     *
     * */
    public function mine_load(){
        if($_POST['my_id']!=null){
            $gold_moving=M('gold_moving');
            $my_message=M('my_message');
            $condition=array();
            $condition['my_id']=$_POST['my_id'];
            $result1=$gold_moving->where($condition)->order('move_time desc')->select();
            $result2=$my_message->where($condition)->order('time desc')->select();
            $have_no_gold_read=0;
            foreach ($result1 as $key =>$i){
                if($result1[$key]['have_read']==0){
                    $have_no_gold_read=1;
                    break;
                }
            }

            $have_no_msg_read=0;
            foreach ($result2 as $key =>$i){
                if($result2[$key]['have_read']==0){
                    $have_no_msg_read=1;
                    break;
                }
            }

            $return_data=array();
            $return_data['error_code']=0;
            $return_data['msg']="数据获取成功";
            $return_data['have_no_gold_read']=$have_no_gold_read;
            $return_data['have_no_msg_read']=$have_no_msg_read;
            $this->ajaxReturn($return_data);
        }

    }

    /*
     * myFiles页面接口
     *
     * */
    //获取所有我的文件
    public function get_my_files(){
        $file_info=M('file_info');

        //设置查询条件
        $condition=array();
        $condition['user_id']=$_POST['user_id'];//根据用户id获取同一用户上传的文件
        //按照时间倒序获取所有文件信息
        $all_files=$file_info->where($condition)->order('file_id desc')->select();

        //将所有的时间戳转换成2011-12-12格式
        foreach($all_files as $key=>$obj){
            $all_files[$key]['file_time']=date('Y-m-d',$obj['file_time']);
        }
        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="数据获取成功";
        $return_data['count']=count($all_files);
        $return_data['data']=$all_files;
        $this->ajaxReturn($return_data);
    }

    //根据前端传来的文件id数组删除对应的文件记录
    public function delete_my_files(){
        //按逗号分离字符串
        $pos = explode(',',$_POST['delfile_id']);

        //循环删除数据库的文件记录
        $file_info=M('file_info');
        $answer_qes=M('answer_qes');
        $gold_moving=M('gold_moving');
        $my_message=M('my_message');
        $qes_list=M('qes_list');

        for($i=0;$i<count($pos);$i++){
            //设置查询条件
            $condition=array();
            $condition['file_id']=(int)$pos[$i];//根据文件id删除文件

            $del=$file_info->where($condition)->find();
            $delFile=unlink($del['file_url']);//删除文件夹文件成功返回true
            //问题表的回答量-1
            if($del['qes_id']!=null){
                $where=array();
                $where['qes_id']=$del['qes_id'];
                $qes=$qes_list->where($where)->find();
                $data=array();
                $data['comment_num']=$qes['comment_num']-1;
                $qes_list->where($where)->save($data);
            }

            $result=$file_info->where($condition)->delete();//删除数据库文件记录
            $answer_qes->where($condition)->delete();
            $gold_moving->where($condition)->delete();
            $my_message->where($condition)->delete();



            if($result!=1||$delFile==false){
                $return_data=array();
                $return_data['error_code']=1;
                $return_data['msg']="文件删除失败";
                $return_data['file_id']=$pos[$i];
                $this->ajaxReturn($return_data);
            }

        }
        $return_data=array();
        $return_data['error_code']=1;
        $return_data['msg']="文件删除成功";
        $return_data['file_count']=count($pos);
        $this->ajaxReturn($return_data);
    }



    /*
     * myFileDetail页面接口
     *
     * */
    //修改文件信息
    public function change_fileInfo(){
        $file_info=M('file_info');
        //查询条件
        $condition=array();
        $condition['file_id']=$_POST['file_id'];

        $data=array();
        $data['type']=$_POST['type'];
        $data['author']=$_POST['author'];
        $data['remark']=$_POST['remark'];
        $data['gold_num']=$_POST['gold_num'];

        $result=$file_info->where($condition)->save($data);//保存修改的文件记录

        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="修改成功";
        $this->ajaxReturn($return_data);
    }



    /*
     * answer页面接口
     *
     * */
    //文件回答
    public function file_answer(){
        //检查前端传过来的参数
        if($_POST['user_id']=="[object Undefined]"){
            $return_data=array();
            $return_data['error_code']=1;
            $return_data['msg']='参数不足: 未获取到用户的id';
            $this->ajaxReturn($return_data);
        }
        if($_POST['file_url']==""){
            $return_data=array();
            $return_data['error_code']=2;
            $return_data['msg']='参数不足: 未获取到临时文件地址';
            $this->ajaxReturn($return_data);
        }

        //$this->ajaxReturn($_FILES['file']['tmp_name']);//小程序传来的文件在系统中的临时文件路径
        if(is_uploaded_file($_FILES['file']['tmp_name'])) {//该临时文件是可以上传的文件
            $file_name=$_POST['file_time'].$_POST['file_name'];//存储文件名为:时间戳+文件名

            //存储地址设置成每个用户有一个文件夹
            $file_path="C:/SharingFiles/".$_POST['user_id'];
            if(!is_dir($file_path))mkdir($file_path);//文件夹是否存在
            $save_path=$file_path."/".$file_name;//存储地址+文件名

            if(move_uploaded_file($_FILES['file']['tmp_name'],$save_path)){// 上传成功
                //将文件数据插入表file_info
                $file_info=M('file_info');
                $data=array();
                $data['user_id']=$_POST['user_id'];
                $data['user_school']=$_POST['user_school'];
                $data['user_faculty']=$_POST['user_faculty'];
                $data['user_name']=$_POST['user_name'];
                $data['user_picurl']=$_POST['user_picurl'];
                $data['qes_id']=$_POST['qes_id'];
                $data['type']=$_POST['type'];
                $data['author']=$_POST['author'];
                $data['remark']=$_POST['remark'];
                $data['gold_num']=$_POST['gold_num'];
                $data['file_url']=$save_path;//文件下载地址
                $data['file_name']=$_POST['file_name'];
                $data['file_time']=$_POST['file_time'];
                $data['file_size']=$_POST['file_size'];
                $data['file_suffix']=$_POST['file_suffix'];
                $res1=$file_info->add($data);//插入数据并获取主键值
                $file_id=$file_info->getLastInsID();

                //将文件回答数据插入到answer_qes表
                $answer_qes=M('answer_qes');
                $ans=array();
                $ans['qes_id']=$_POST['qes_id'];
                $ans['type']=2;
                $ans['file_id']=$file_id;
                $res2=$answer_qes->add($ans);//插入数据

                //提醒 问题提出者
                $my_message=M('my_message');
                $msg=array();
                $msg['other_id']=$_POST['user_id'];
                $msg['other_picurl']=$_POST['user_picurl'];
                $msg['qes_id']=$_POST['qes_id'];
                $msg['qes_content']=$_POST['qes_content'];
                $msg['type']=0;//代表文件回答
                $msg['file_id']=$file_id;
                $msg['other_name']=$_POST['user_name'];
                $msg['time']=$_POST['file_time'];

                $msg['my_id']=$_POST['qesUser_id'];
                $msg['follow']=0;//提问者
                $mr=$my_message->add($msg);

                //提醒问题关注者
                $where=array();
                $where['qes_id']=$_POST['qes_id'];
                $where['type']=0;//查找所有关注者

                $follower=$answer_qes->where($where)->select();
                $msg['follow']=1;//关注者
                foreach ($follower as $i){
                    $msg['my_id']=$i['follow_id'];
                    $mr=$my_message->add($msg);
                }

                //问题的回答数+1
                $qes_list=M('qes_list');
                $qq=array();
                $qq['qes_id']=$_POST['qes_id'];
                $qres=$qes_list->where($qq)->find();
                $up=array();
                $up['comment_num']=$qres['comment_num']+1;
                $res3=$qes_list->where($qq)->save($up);

                if($res1&&$res2&&$res3){
                    $return_data=array();
                    $return_data['error_code']=0;
                    $return_data['msg']='文件上传成功';
                    $this->ajaxReturn($return_data);
                }else{
                    $return_data=array();
                    $return_data['error_code']=3;
                    $return_data['msg']='数据添加失败';
                    $this->ajaxReturn($return_data);
                }
            }
            $return_data=array();
            $return_data['error_code']=3;
            $return_data['msg']='文件上传失败';
            $this->ajaxReturn($return_data);
        }
        $return_data=array();
        $return_data['error_code']=3;
        $return_data['msg']='无法上传该文件';
        $this->ajaxReturn($return_data);
    }

    //评论回答
    public function comment_answer(){
        //检查前端传过来的参数
        if($_POST['user_id']=="[object Undefined]"){
            $return_data=array();
            $return_data['error_code']=1;
            $return_data['msg']='参数不足: 未获取到用户的id';
            $this->ajaxReturn($return_data);
        }

        //将评论数据插入表comment_info
        $comment_info=M('comment_info');
        $data=array();
        $data['user_id']=$_POST['user_id'];
        $data['user_school']=$_POST['user_school'];
        $data['user_faculty']=$_POST['user_faculty'];
        $data['user_name']=$_POST['user_name'];
        $data['user_picurl']=$_POST['user_picurl'];
        $data['qes_id']=$_POST['qes_id'];
        $data['content']=$_POST['content'];
        $data['time']=$_POST['time'];
        $res1=$comment_info->add($data);//插入数据并获取主键值
        $id=$comment_info->getLastInsID();

        //将评论回答数据插入到answer_qes表
        $answer_qes=M('answer_qes');
        $ans=array();
        $ans['qes_id']=$_POST['qes_id'];
        $ans['type']=1;
        $ans['comment_id']=$id;
        $res2=$answer_qes->add($ans);//插入数据

        //提醒 问题提出者
        $my_message=M('my_message');
        $msg=array();
        $msg['other_id']=$_POST['user_id'];
        $msg['other_picurl']=$_POST['user_picurl'];
        $msg['qes_id']=$_POST['qes_id'];
        $msg['qes_content']=$_POST['qes_content'];
        $msg['type']=1;//代表评论回答
        $msg['comment_id']=$id;
        $msg['other_name']=$_POST['user_name'];
        $msg['time']=$_POST['time'];

        $msg['my_id']=$_POST['qesUser_id'];
        $msg['follow']=0;//提问者
        $mr=$my_message->add($msg);

        //提醒问题关注者
        $where=array();
        $where['qes_id']=$_POST['qes_id'];
        $where['type']=0;//查找所有关注者

        $follower=$answer_qes->where($where)->select();
        $msg['follow']=1;//关注者
        foreach ($follower as $i){
            $msg['my_id']=$i['follow_id'];
            $mr=$my_message->add($msg);
        }

        //问题的回答数+1
        $qes_list=M('qes_list');
        $qq=array();
        $qq['qes_id']=$_POST['qes_id'];
        $qres=$qes_list->where($qq)->find();
        $up=array();
        $up['comment_num']=$qres['comment_num']+1;
        $res3=$qes_list->where($qq)->save($up);

        if($res1&&$res2&&$res3){
            $return_data=array();
            $return_data['error_code']=0;
            $return_data['msg']='评论成功';
            $this->ajaxReturn($return_data);
        }else{
            $return_data=array();
            $return_data['error_code']=2;
            $return_data['msg']='数据添加失败';
            $this->ajaxReturn($return_data);
        }
    }




    /*
     * message页面接口
     *
     * */
    public function get_message(){
        $my_message=M('my_message');
        $condition=array();
        $condition['my_id']=$_POST['my_id'];
        $result=$my_message->where($condition)->order('time desc')->select();

        $report_list=M('report_list');
        $where=array();

        foreach ($result as $key =>$i){
                //转化时间戳
                $result[$key]['time'] = date('Y-m-d H:i',$i['time']);


        }

        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']="数据获取成功";
        $return_data['data']=$result;
        $this->ajaxReturn($return_data);
    }

    public function readReport(){
        $where=array();
        $where['msg_id']=$_POST['msg_id'];
        $my_message=M('my_message');

        $result=$my_message->where($where)->find();
        if($result['have_read']==0){
            $data=array();
            $data['have_read']=1;
            $result=$my_message->where($where)->save($data);
        }

    }




    /*
     * msgAnswer页面接口
     *
     * */
    public function get_msg_answer(){
        $where = array();
        $where['qes_id'] = $_POST['qes_id'];

        $qeslist = M('qes_list');
        $result = $qeslist->where($where)->find();

        $return_data=array();
        if($result) {
            //转化时间戳
            $result['qes_time'] = date('Y-m-d', $result['qes_time']);

            $comment_info = M('comment_info');
            $file_info = M('file_info');

            //显示已读
            if($_POST['have_read']=="0"){
                $my_message=M('my_message');
                $where=array();
                $where['msg_id']=(int)$_POST['msg_id'];

                $data=array();
                $data['have_read']=1;
                $res=$my_message->where($where)->save($data);
            }

            if ($_POST['file_id'] != "null") {//文件回答
                $w = array();
                $w['file_id'] = $_POST['file_id'];
                $file = $file_info->where($w)->find();
                $return_data['error_code'] = 0;
                $return_data['msg'] = '获取数据成功';
                $return_data['question']=$result;
                $return_data['fileInfo'] = $file;
                $this->ajaxReturn($return_data);
            } else if ($_POST['comment_id'] != "null") {
                $w = array();
                $w['id'] = $_POST['comment_id'];
                $comment = $comment_info->where($w)->find();
                $comment['zan']=true;
                $return_data['error_code'] = 0;
                $return_data['msg'] = '获取数据成功';
                $return_data['question']=$result;
                $return_data['comment'] = $comment;
                $this->ajaxReturn($return_data);
            }

        }
        $return_data['error_code'] = 1;
        $return_data['msg'] = '出错了！';
        $this->ajaxReturn($return_data);
    }


    /*
     * qesDetail页面接口
     *
     * */
    //关注或取消关注
    public function follow_or_not(){
        //检查前端传过来的参数
        if($_POST['follow_id']=="[object Undefined]"){
            $return_data=array();
            $return_data['error_code']=1;
            $return_data['msg']='参数不足: 未获取到用户的id';
            $this->ajaxReturn($return_data);
        }

        if($_POST['followHidden']=="true"){//关注问题
            //数据库添加信息
            //将评论回答数据插入到answer_qes表
            $answer_qes=M('answer_qes');
            $ans=array();
            $ans['qes_id']=$_POST['qes_id'];
            $ans['type']=0;
            $ans['follow_id']=$_POST['follow_id'];
            $res=$answer_qes->add($ans);//插入数据

            //问题的关注数+1
            $qes_list=M('qes_list');
            $qq=array();
            $qq['qes_id']=$_POST['qes_id'];
            $qres=$qes_list->where($qq)->find();
            $up=array();
            $up['collect_num']=$qres['collect_num']+1;
            $res3=$qes_list->where($qq)->save($up);

            $return_data=array();
            $return_data['error_code']=0;
            $return_data['msg']='关注成功';
            $this->ajaxReturn($return_data);
        }
        else{//取消关注
            //数据库删除信息
            $answer_qes=M('answer_qes');
            $ans['qes_id']=$_POST['qes_id'];
            $ans['follow_id']=$_POST['follow_id'];
            $res=$answer_qes->where($ans)->delete();//删除数据

            //问题的关注数-1
            $qes_list=M('qes_list');
            $qq=array();
            $qq['qes_id']=$_POST['qes_id'];
            $qres=$qes_list->where($qq)->find();
            $up=array();
            $up['collect_num']=$qres['collect_num']-1;
            $res3=$qes_list->where($qq)->save($up);

            if($res){
                $return_data=array();
                $return_data['error_code']=0;
                $return_data['msg']='取消关注成功';
                $this->ajaxReturn($return_data);
            }
            else{
                $return_data=array();
                $return_data['error_code']=2;
                $return_data['msg']='未找到该用户关注的问题';
                $this->ajaxReturn($return_data);
            }
        }
    }

    //页面初始化
    public function get_qes_answer(){

        if(!$_POST['qes_id']){
            $return_data = array();
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足:qes_id';
            $this->ajaxReturn($return_data);
        }

        $where = array();
        $where['qes_id'] = $_POST['qes_id'];

        $qeslist = M('qes_list');
        $result = $qeslist->where($where)->find();

        $return_data=array();
        if($result){
            //转化时间戳
            $result['qes_time'] = date('Y-m-d',$result['qes_time']);

            //在answer_qes查询该问题id下的所有文件和评论
            $answer_qes=M('answer_qes');
            $comment_info=M('comment_info');
            $file_info=M('file_info');

            $condition=array();
            $condition['qes_id']=$_POST['qes_id'];
            $condition['type']=array(1,2,'or');//同一字段多种条件查询

            $res=$answer_qes->where($condition)->order('id desc')->select();
            //根据file_id在文件表中查找对应文件并与评论记录一起拼接在同一数组
            $display=array();$c=0;
            foreach($res as $i){
                if($i['type']=="1"){//评论回答
                    $w=array();
                    $w['id']=$i['comment_id'];
                    $display[$c]=$comment_info->where($w)->find();
                    $display[$c]['time']=date('Y-m-d',$display[$c]['time']);
                    $display[$c]['zan']=true;//用来前端判断是否赞了
                }
                else if($i['type']=="2"){//文件回答
                    $w=array();
                    $w['file_id']=$i['file_id'];
                    $display[$c]=$file_info->where($w)->find();
                    $display[$c]['file_time']=date('Y-m-d',$display[$c]['file_time']);
                }
                $c++;
            }

            //判断该用户是否关注该问题
            $f=array();
            $f['qes_id']=$_POST['qes_id'];
            $f['follow_id']=$_POST['user_id'];

            $res=$answer_qes->where($f)->find();
            $follow=false;
            if($res){
                $follow=true;
            }

            $return_data['error_code'] = 0;
            $return_data['msg'] = '获取数据成功';
            $return_data['qes'] = $result;
            $return_data['data']=$display;
            $return_data['count']=count($display);
            $return_data['follow']=$follow;
            $this->ajaxReturn($return_data);

        }else{
            $return_data['error_code'] = 2;
            $return_data['msg'] = '该问题不存在';
            $return_data['qes_id'] = $_POST['qes_id'];
            $this->ajaxReturn($return_data);
        }
    }

    //评论点赞
    public function comment_like(){
        $comment_info=M('comment_info');
        $condition=array();
        $condition['id']=$_POST['id'];
        if($_POST['zan']=="false"){//取消赞
            $data=array();
            $data['like_num']=$_POST['like_num']-1;
            $res1=$comment_info->where($condition)->save($data);
            $return_data=array();
            $return_data['error_code']=0;
            $return_data['msg']='取消成功';
            $this->ajaxReturn($return_data);
        }
        else{//点赞
            $data=array();
            $data['like_num']=$_POST['like_num']+1;
            $res2=$comment_info->where($condition)->save($data);
            $return_data=array();
            $return_data['error_code']=0;
            $return_data['msg']='点赞成功';
            $this->ajaxReturn($return_data);
        }

    }

    //本学校回答
    public function get_mySchool_answer(){
        //在answer_qes查询该问题id下的所有文件和评论
        $answer_qes=M('answer_qes');
        $comment_info=M('comment_info');
        $file_info=M('file_info');

        $condition=array();
        $condition['qes_id']=$_POST['qes_id'];
        $condition['type']=array(1,2,'or');//同一字段多种条件查询

        $res=$answer_qes->where($condition)->order('id desc')->select();
        //根据file_id在文件表中查找对应文件并与评论记录一起拼接在同一数组
        $display=array();$c=0;
        foreach($res as $i){
            if($i['type']=="1"){//本学校的评论回答
                $w=array();
                $w['id']=$i['comment_id'];
                $result=$comment_info->where($w)->find();
                if($result['user_school']==$_POST['user_school']){
                    $display[$c]=$result;
                    $display[$c]['time']=date('Y-m-d',$display[$c]['time']);
                    $display[$c]['zan']=true;//用来前端判断是否赞了
                    $c++;
                }

            }
            else if($i['type']=="2"){////本学校的文件回答
                $w=array();
                $w['file_id']=$i['file_id'];
                $result=$file_info->where($w)->find();
                if($result['user_school']==$_POST['user_school']){
                    $display[$c]=$result;
                    $display[$c]['file_time']=date('Y-m-d',$display[$c]['file_time']);
                    $c++;
                }

            }

        }

        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']='数据获取成功';
        $return_data['data']=$display;
        $return_data['count']=count($display);
        $this->ajaxReturn($return_data);
    }

    //仅评论
    public function get_only_comment(){
        //在answer_qes查询该问题id下的所有文件和评论
        $answer_qes=M('answer_qes');
        $comment_info=M('comment_info');

        $condition=array();
        $condition['qes_id']=$_POST['qes_id'];
        $condition['type']=1;

        $res=$answer_qes->where($condition)->order('id desc')->select();
        //根据file_id在文件表中查找对应文件并与评论记录一起拼接在同一数组
        $display=array();$c=0;
        foreach($res as $i){
            $w=array();
            $w['id']=$i['comment_id'];
            $display[$c]=$comment_info->where($w)->find();
            $display[$c]['time']=date('Y-m-d',$display[$c]['time']);
            $display[$c]['zan']=true;//用来前端判断是否赞了
            $c++;
        }

        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']='数据获取成功';
        $return_data['data']=$display;
        $return_data['count']=count($display);
        $this->ajaxReturn($return_data);
    }

    //仅文件
    public function get_only_files(){
        //在answer_qes查询该问题id下的所有文件和评论
        $answer_qes=M('answer_qes');
        $file_info=M('file_info');

        $condition=array();
        $condition['qes_id']=$_POST['qes_id'];
        $condition['type']=2;
        $res=$answer_qes->where($condition)->order('id desc')->select();
        //根据file_id在文件表中查找对应文件并与评论记录一起拼接在同一数组
        $display=array();$c=0;
        foreach($res as $i){
            $w=array();
            $w['file_id']=$i['file_id'];
            $display[$c]=$file_info->where($w)->find();
            $display[$c]['file_time']=date('Y-m-d',$display[$c]['file_time']);
            $c++;
        }

        $return_data=array();
        $return_data['error_code']=0;
        $return_data['msg']='数据获取成功';
        $return_data['data']=$display;
        $return_data['count']=count($display);
        $this->ajaxReturn($return_data);
    }
}

